<?php

namespace app\admin\controller\print_md;

use app\data\model\DataAddresser;
use app\data\model\DataCity;
use think\admin\Controller;
use think\admin\helper\QueryHelper;

class Addresser extends Controller
{
    protected $default_type = 'json';
    public function index(){
        DataAddresser::mQuery()->where(['del'=>0])->layTable(function () {
            $this->title = '发件人管理';
        }, function (QueryHelper $query) {

        });
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(array &$data)
    {
         foreach ($data as &$dv){
            $dv['province_name'] = DataCity::getProvince($dv['province_id']);
            $dv['city_name'] = DataCity::getProvince($dv['city_id']);
            $dv['district_name'] = DataCity::getProvince($dv['district_id']);
        }
    }

    public function add(){
        if($this->request->isPost()){
            $post = $this->request->post();
            $this->_vali(['name.require' => '参数不能为空！']);
            $this->_vali(['mobile.require' => '参数不能为空！']);
            $this->_vali(['mobile.mobile' => '参数格式有误！']);
            $this->_vali(['address.require' => '参数不能为空！']);
            DataAddresser::create(array(
                'name'          => $post['name'],
                'mobile'        => $post['mobile'],
                'province_id'   => $post['province_id']??'',
                'city_id'       => $post['city_id']??'',
                'district_id'   => $post['district_id']??'',
                'address'       => $post['address']
            ));
            $this->success('添加发件人成功');
        }
        //获取省份
        $province = DataCity::getLists('id,name,adcode',['level'=>1]);
        $this->province = $province;
        $this->fetch();
    }
    public function edit(){
        if($this->request->isPost()){
            $post = $this->request->post();
            $this->_vali(['name.require' => '参数不能为空！']);
            $this->_vali(['mobile.require' => '参数不能为空！']);
            $this->_vali(['mobile.mobile' => '参数格式有误！']);
            $this->_vali(['address.require' => '参数不能为空！']);
            DataAddresser::update(array(
                'name'          => $post['name'],
                'mobile'        => $post['mobile'],
                'province_id'   => $post['province_id']??'',
                'city_id'       => $post['city_id']??'',
                'district_id'   => $post['district_id']??'',
                'address'       => $post['address']
            ),['id'=>$post['id']]);
            $this->success('编辑发件人成功');
        }
        $id = $this->request->get('id');
        //获取省份
        $province = DataCity::getLists('id,name,adcode',['level'=>1]);
        $info = DataAddresser::getDetail('*',['id'=>$id]);
        if(empty($info) === FALSE){
            $info['city_name'] = DataCity::getCity($info['city_id']);
            $info['district_name'] = DataCity::getDistrict($info['district_id']);
        }
        $this->info = $info;
        $this->id = $id;
        $this->province = $province;
        $this->fetch();
    }
    public function del(){
        $id = $this->request->get('id');
        DataAddresser::update(array(
                'del'          => 1
            ),['id'=>$id]);
            $this->success('删除发件人成功');
    }
    public function getCity(){
        $post = $this->request->post();
        $pid = $post['pid'];
        $lists = DataCity::getLists('id,adcode,name',['p_adcode'=>$pid]);
        return \Tools::returnMsg(true,$lists,$this->default_type);
    }
}
